/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 * 
 *	Created on 2010-12-13 下午05:16:14
 */
package cn.com.ccxe.core.db.util;

import java.io.Serializable;
import java.util.Comparator;

import cn.com.ccxe.core.entity.Article;
import cn.com.ccxe.core.util.CodeUtil;

import com.sleepycat.bind.tuple.TupleInput;

public class ArticleCompartor implements Comparator<byte[]>,Serializable{

	private static final long serialVersionUID = -3584103388615342425L;
	
	/**
	 * TODO 比较标准与测试文档的编写。
	 */
	public int compare(byte[] o1, byte[] o2) {
		Article a1 = new ArticleTupleBingding().entryToObject(new TupleInput(o1));
		Article a2 = new ArticleTupleBingding().entryToObject(new TupleInput(o2));
		if(a1.getUrl().equals(a2.getUrl()) 
			&& a1.getModuleId() == a2.getModuleId()) {
			return 0;
		}
		if(a1.getModuleId() != a2.getModuleId()) {
			return a1.getModuleId() - a2.getModuleId();
		}
		return code(a1.getUrl()) - code(a2.getUrl());
	}
	
	private int code(String str) {
		return CodeUtil.byteArray2Int(str.getBytes());
	}

}
